﻿//-----------------------------------------------------------------------
// <copyright file="Program.cs" company="DylanBox">
// Copyright (c) DylanBox Corporation. All rights reserved.
// </copyright>
// <summary>Recurisve algorithm</summary>
// <reference>
//      http://www.simple-talk.com/dotnet/.net-framework/.net-developer-basics-%e2%80%93-recursive-algorithms/
//      http://www.cnblogs.com/DylanWind/archive/2010/05/09/1730842.html
// </reference>
// <author>Dylan</author>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Recursive
{
    class Program
    {
        static int counts = 0;
        static void Main(string[] args)
        {
            Hanoi(4, 'a', 'c', 'b');

            int[] store;
            store = new int[10];
            int sum = 0;
            ShotScore.Cumput(90, 9, store);

            sum = ShotScore.sum2();



            CountDown(5);

            CountUp(5);

            Console.WriteLine(Factorial(6)); //720
            Console.WriteLine(Fibonacci(6)); //8
            Console.Read();          
        }

        /// <summary>
        /// Winding recurisve to CountDown
        /// </summary>
        /// <param name="num"></param>
        public static void CountDown(int num)
        {
            Console.WriteLine(num);

            if (num>0)
            {
                CountDown(num - 1);
            }
        }

        /// <summary>
        /// UnWinding recursion
        /// </summary>
        /// <param name="num"></param>
        public static void CountUp(int num)
        {  
            if (num > 0)
            {
                CountUp(num - 1);
            }
            Console.WriteLine(num);
        }

        /// <summary>
        /// Caculate factorial of num
        /// 0! = 1　　n! = n * (n-1)!
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static int Factorial(int num)
        {
            if (num == 0)
            {
                return 1;
            }

            return num * Factorial(num - 1);            
        }

        /// <summary>
        /// Show off in an Interview, A Good Factorial
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static UInt64 GoodFactorial(byte num)
        {
            return (0 == num) ? 1 : num * GoodFactorial(Byte.Parse((num-1).ToString()));
        }

        /// <summary>
        /// Fibonacci数列
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static int Fibonacci(int num)
        {
            if (num == 0)
            {
                return 0;
            }
            else if (num == 1)
            {
                return 1;
            }
            else
            {
                return Fibonacci(num - 2) + Fibonacci(num - 1);
            }
        }

        /// <summary>
        /// 汉诺塔
        /// 问题可以描述为：
        ///   将n个片从A移到C上，B是中介
        ///    1. 首先把A上的n-1个片通过C移到B上
        ///    2. 把A上剩余哪个最大的片移到C上，
        ///    3, 把B上n-1个片通过A移到C上
        /// </summary>
        /// <param name="n"></param>
        /// <param name="a"></param>
        /// <param name="c"></param>
        /// <param name="b"></param>
        public static void Hanoi(int n, char a, char c, char b)
        {
            if (n == 1)
            {
                Move(a, c);
            }
            else
            {
                Hanoi(n - 1, a, b, c);
                Move(a, c);
                Hanoi(n - 1, b, c, a);   
            }    
        }

        private static void Move(char src, char des)
        {
            Console.WriteLine(string.Format("Move from {0} to {1}", src, des));
        }


    }
}
